package com.bwie.doctor.service.impl;

import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.doctor.domain.dto.AddArticleDTO;
import com.bwie.doctor.domain.dto.UpdArticleDTO;
import com.bwie.doctor.domain.vo.ArticleDetailVo;
import com.bwie.doctor.domain.vo.ArticleDraftVo;
import com.bwie.doctor.domain.vo.ArticleListVo;
import com.bwie.doctor.mapper.DoctorArticleMapper;
import com.bwie.doctor.service.DoctorArticleService;
import com.bwie.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorArticleServiceServiceImpl implements DoctorArticleService {
    /**
     * 注入
     */
    @Autowired
    private DoctorArticleMapper mapper;


    /**
     * 医生获得所有文章列表
     * @return
     */
    @Override
    public List<ArticleListVo> getAllArticle() {
        //获得所有文章列表
        List<ArticleListVo> list = mapper.getAllArticle();
        //返回
        return list;
    }

    /**
     * 医生添加文章/草稿
     * @param addArticleDTO
     */
    @Override
    public void addArticle(AddArticleDTO addArticleDTO) {
        //创建判断值
        int i =0;
        //获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //判断添加的是什么类型的文章
        if (addArticleDTO.getArticleType()==1){
            //医生添加视频文章
            i = mapper.addVideoArticle(addArticleDTO,loginUser.getUserid());
        }else{
            //医生添加文字文章/草稿
            i = mapper.addArticle(addArticleDTO,loginUser.getUserid());
        }
        //判断是否添加成功
        if (i<=0){
            //抛异常
            throw new RuntimeException("添加失败");
        }
    }

    /**
     * 获得医生文章草稿列表
     * @return
     */
    @Override
    public List<ArticleDraftVo> getDraft() {
        //获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获得所有文章列表
        List<ArticleDraftVo> list = mapper.getDraft(loginUser.getUserid());
        //返回
        return list;
    }

    /**
     * 获得医生发布的文章列表
     * @return
     */
    @Override
    public List<ArticleListVo> getDoctorVideoArticle(Integer type) {
        //获得登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获得医生发布的视频文章列表
        List<ArticleListVo> list = mapper.getDoctorVideoArticle(loginUser.getUserid(),type);
        //返回
        return list;
    }

    /**
     * 医生删除草稿
     * @param id
     */
    @Override
    public void delDraft(Long id) {
        //根据id查询文章草稿信息

        //删除草稿
        int i = mapper.delDraft(id);
        //判断是否删除成功
        if (i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 医生修改草稿
     * @param updArticleDTO
     */
    @Override
    public void updDraft(UpdArticleDTO updArticleDTO) {
        //创建判断值
        int i = 0;
        //判断文章类型
        if (updArticleDTO.getArticleType()==1){
            //修改草稿
            i = mapper.updVideoArticle(updArticleDTO);
        }
        //修改草稿
        i = mapper.updArticle(updArticleDTO);
    }

    /**
     * 医生根据id搜索文章详情
     * @param id
     * @return
     */
    @Override
    public ArticleDetailVo getArticleDetail(Long id) {
        //根据id搜索文章详情
        ArticleDetailVo articleDetailVo = mapper.getArticleDetail(id);
        //判断是否为空
        if (articleDetailVo==null){
            //抛异常
            throw new RuntimeException("文章信息出错");
        }
        //返回
        return articleDetailVo;
    }


}
